package com.aia.prm.web.ria.app.ui;

import com.aia.prm.web.ria.app.MyMenu;
import com.aia.prm.web.ria.app.PrmApplication;
import com.aia.prm.web.ria.app.ui.config.ConfigView;
import com.aia.prm.web.ria.app.ui.group.GroupView;
import com.aia.prm.web.ria.app.ui.pext.PextView;
import com.aia.prm.web.ria.app.ui.plan.PlanView;
import com.cfh.domain.User;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.themes.Reindeer;

public class G2View extends VerticalLayout {
	private static final long serialVersionUID = 1L;
	final PrmApplication app;
	private TabSheet tabSheet;
	// private MenuBar menubar = new MenuBar();

	private MyMenu myMenu;

	private Command menuCommand = new Command() {
		public void menuSelected(MenuItem selectedItem) {
			addTab(new DemoView(), "功能1");
		}
	};

	public G2View(PrmApplication argapp) {
		this.app = argapp;
		myMenu = new MyMenu(this, app);
		setSizeFull();

		HorizontalLayout infoBar = new HorizontalLayout();
		infoBar.setWidth(100, UNITS_PERCENTAGE);
		infoBar.setSpacing(true);

		Button changePassword = new Button("修改密码", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				ChangePasswordDialog dialog = new ChangePasswordDialog(app);
				app.getMainWindow().addWindow(dialog);
			}
		});
		changePassword.setIcon(new ThemeResource("icons/lock.png"));

		Button disconnectButton = new Button("注销", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				G2View.this.getApplication().setUser(null);
				G2View.this.getApplication().close();
			}
		});
		disconnectButton.setIcon(new ThemeResource("icons/shut_down.png"));

		changePassword.setStyleName(BaseTheme.BUTTON_LINK);
		disconnectButton.setStyleName(BaseTheme.BUTTON_LINK);
		User user = (User) this.app.getUser();
		System.out.println("user = " + this.app.getUser());
		Label welcomeLabel = new Label("欢迎您, " + user.getUSER_NAME());

		// final MenuBar.MenuItem systemMenu = menubar.addItem("系统菜单", null);
		// systemMenu.setIcon(new ThemeResource("icons/applications.png"));
		// systemMenu.addItem("参数配置", new Command() {
		// public void menuSelected(MenuItem selectedItem) {
		// addTab(new ConfigView(app), selectedItem.getText());
		// }
		// });
		//
		// systemMenu.addSeparator();
		// systemMenu.addItem("产品基本信息配置", new Command() {
		// public void menuSelected(MenuItem selectedItem) {
		// addTab(new PlanView(app), selectedItem.getText());
		// }
		// });
		//
		// systemMenu.addItem("产品扩展信息配置", new Command() {
		// public void menuSelected(MenuItem selectedItem) {
		// addTab(new PextView(app), selectedItem.getText());
		// }
		// });
		//
		// systemMenu.addItem("产品组合代码配置", new Command() {
		// public void menuSelected(MenuItem selectedItem) {
		// addTab(new GroupView(app), selectedItem.getText());
		// }
		// });

		// final MenuBar.MenuItem helpMenu = menubar.addItem("帮助", null);
		// helpMenu.setIcon(new ThemeResource("icons/help.png"));
		// helpMenu.addItem("关于Administrator console", menuCommand);

		infoBar.addComponent(myMenu.buildMenuBar());
		infoBar.addComponent(welcomeLabel);
		infoBar.addComponent(changePassword);
		infoBar.addComponent(disconnectButton);
		infoBar.addComponent(new Label("&bnsp;&bnsp;"));
		infoBar.setExpandRatio(welcomeLabel, 1);

		tabSheet = new TabSheet();
		tabSheet.setSizeFull();
		tabSheet.addStyleName(Reindeer.TABSHEET_HOVER_CLOSABLE);
		// VerticalLayout rootLayout = new VerticalLayout();
		addComponent(infoBar);
		addComponent(tabSheet);
		setExpandRatio(tabSheet, 1);
		setSizeFull();

		// setContent(rootLayout);
	}

	public void addTab(Component component, String name) {
		Tab tab = tabSheet.addTab(component, name, new ThemeResource("icons/process.png"));
		tab.setClosable(true);
		tabSheet.setSelectedTab(component);
	}

	public Component fetchSelectView() {
		if (tabSheet == null)
			return null;
		return tabSheet.getSelectedTab();
	}

}